
    ##########################################################################################
    ### R code for Figure 9 of Supplemental Material
    ##########################################################################################
    
    ### Note: For some reason, anominate works in 64-bit R only!!!

    library(anominate)
    library(wnominate)
    library(pscl)

    Legislators <- read.fwf("hou103kh.ord", header=F, widths=c(3,5,2,2,7,4,1,1,11))
    All <- read.fwf("hou103kh.ord", header=F, widths=rep(1,1130))

    Votes <- All[ , 37:rcol(All)] 
    
    Info <- Legislators[,c(2,3,4,6)]
    row.names(Info) <- Legislators[,9]
    colnames(Info) <- c("ICPSR_ID", "ICPSR_state", "CD", "party") 
    Info.a <- Info[,c(1,3)] 
    Dat <- Legislators 
    Dat$legis.data <- Info
    Dat$votes <- Votes 
    
    Data1 <- rollcall(data=Dat$votes, legis.data=Dat$legis.data,
                     yea=c(1,2,3),
                     nay=c(4,5,6),
                     missing=c(7,8,9),
                     notInLegis=0)
    
    # Now Run ANOMINATE
    result <- anominate(Data1, dims=1, nsamp=2000, thin=1, burnin=1000, 
                        minvotes=5, lop=0.025, polarity=13,  random.starts=FALSE,  verbose=T) # polarity=13 sets Kyl (13th row) to be positive.

    out_ideal <- result$legislators[[1]]
    quantiles_out <- apply(out_ideal, 2, FUN=quantile, probs=c(0.025, 0.5, 0.975))
    mean_out <- apply(out_ideal, 2, FUN=mean)
    summary_results <- cbind(t(quantiles_out), mean_out)

    ### Decode the lable of summary_results to extract out the row number of the legislators
    original.label <- rownames(summary_results)    
    row_number <- rep(NA, length(original.label))
    for(j in 1:length(original.label)){
    row_number[j] <- as.numeric(strsplit(original.label, split="r ")[[j]][2])
    }

    select.info <- Info.a[row_number, ]    
   
    Info_dynamic <- data.frame(select.info, summary_results)
    Info_dynamic$congress <- rep(103, nrow(Info_dynamic))
    colnames(Info_dynamic) <- c("ICPSR_ID","CD", "anom.CI1", "anom.median",  "anom.CI2",   "anom.mean", "congress")


    ### Merge with the existing file with all measures
    File1 <- read.csv("Supplemental_Figure9_Data1.csv")
    Combined <- merge(Info_dynamic, File1, by=c("ICPSR_ID"), all=F)

    postscript("Supplemental_Figure9.eps", height=12, width=5, horizontal=F)
    par(mfrow=c(2, 1))
    plot(Combined$IRT_mean, Combined$NOM_dim2, type='p', col='black', xlim=c(-3, 3), ylim=c(-1, 1), xlab="IRT Estimate", ylab="W-NOMINATE Dim 2")
    segments(-3, -1, 3, 1, lty=1)
    plot(Combined$IRT_mean, Combined$anom.mean, type='p', col='black', xlim=c(-3, 3), ylim=c(-3, 3), xlab="IRT Estimate", ylab="A-NOMINATE")
    segments(-3, -3, 3, 3, lty=1)
    dev.off()    


